package com.lemon.secondhandplatform.ui.activity.user;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lemon.secondhandplatform.BaseApplication;
import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.User;
import com.lemon.secondhandplatform.ui.activity.BaseActivity;
import com.lemon.secondhandplatform.ui.util.StatusBarUtils;
import com.lemon.secondhandplatform.util.PathGetter;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UploadFileListener;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 个人信息
 */
public class UserActivity extends BaseActivity {

    private User user;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.imgAvator)
    CircleImageView imgAvator;
    @BindView(R.id.tvNickname)
    TextView tvNickname;
    @BindView(R.id.tvSex)
    TextView tvSex;
    @BindView(R.id.tvPhone)
    TextView tvPhone;
    @BindView(R.id.tvEmail)
    TextView tvEmail;
    @BindView(R.id.tvBirthday)
    TextView tvBirthday;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        bindService();

        user = BaseApplication.getUser();
        initView();
        bindListener();
    }

    //请求码：编辑头像
    public static final int OPEN_GALLERY = 1;

    /**
     * 编辑头像
     *
     * @param v
     */
    @OnClick(R.id.avator)
    public void editAvator(View v) {
        Intent intent = new Intent();
        //选择图片格式
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, OPEN_GALLERY);
    }

    //请求码：编辑昵称
    public static final int EDIT_USER_NICKNAME = 10;

    /**
     * 编辑昵称
     *
     * @param v
     */
    @OnClick(R.id.nickname)
    public void editNickname(View v) {
        String nickname = tvNickname.getText().toString();
        Intent intent = Edit.newIntent(this, "昵称", nickname);
        startActivityForResult(intent, EDIT_USER_NICKNAME);
    }

    //请求码：编辑性别
    public static final int EDIT_USER_SEX = 10000;

    /**
     * 编辑性别
     *
     * @param v
     */
    @OnClick(R.id.sex)
    public void editSex(View v) {
        String sex = tvSex.getText().toString();
        Intent intent = Edit.newIntent(this, "性别", sex);
        startActivityForResult(intent, EDIT_USER_SEX);
    }

    //请求码：编辑电话
    public static final int EDIT_USER_PHONE = 11;

    /**
     * 编辑电话
     *
     * @param v
     */
    @OnClick(R.id.phone)
    public void editPhone(View v) {
        String phone = tvPhone.getText().toString();
        Intent intent = Edit.newIntent(this, "电话", phone, Edit.PHONE);
        startActivityForResult(intent, EDIT_USER_PHONE);
    }

    //请求码：编辑邮箱
    public static final int EDIT_USER_EMAIL = 100;

    /**
     * 编辑邮箱地址
     *
     * @param v
     */
    @OnClick(R.id.email)
    public void editEmail(View v) {
        String email = tvEmail.getText().toString();
        Intent intent = Edit.newIntent(this, "邮箱", email);
        startActivityForResult(intent, EDIT_USER_EMAIL);
    }

    /**
     * 编辑生日
     *
     * @param v
     */
    @OnClick(R.id.birthday)
    public void editBirthday(View v) {

    }

    /**
     * 退出登录
     *
     * @param v
     */
    @OnClick(R.id.logout)
    public void logout(View v) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            //头像
            case OPEN_GALLERY:
                if (resultCode == RESULT_OK) {
                    Uri uri = data.getData();
                    try {
                        //通过uri获取到bitmap对象
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                        imgAvator.setImageBitmap(bitmap);

                        //通过uri获取图片路径
                        String path = PathGetter.getPath(this, uri);
                        File file = new File(path);
                        final BmobFile bmobFile = new BmobFile(file);
                        bmobFile.uploadblock(new UploadFileListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null) {
                                    user.setAvator(bmobFile.getFileUrl());
                                    binder.editUser(user);
                                }
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;

            //昵称
            case EDIT_USER_NICKNAME:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra(Edit.EXTRA_DATA);
                    tvNickname.setText(result);
                    user.setNickname(result);
                }
                break;

            //性别
            case EDIT_USER_SEX:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra(Edit.EXTRA_DATA);
                    tvSex.setText(result);
                    user.setSex(result);
                }
                break;

            //电话
            case EDIT_USER_PHONE:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra(Edit.EXTRA_DATA);
                    tvPhone.setText(result);
                    try {
                        user.setPhone(Integer.valueOf(result));
                    } catch (NumberFormatException e) {
                        user.setPhone(null);
                    }
                }
                break;

            //邮箱
            case EDIT_USER_EMAIL:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra(Edit.EXTRA_DATA);
                    tvEmail.setText(result);
                    user.setEmail(result);
                }
                break;
        }
        binder.editUser(user);

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initView() {
        StatusBarUtils.drawableStatus(this, R.color.colorPrimary);

        Glide.with(this)
                .load(user.getAvator())
                .into(imgAvator);
        tvNickname.setText(user.getNickname());
        tvSex.setText(user.getSex());
        Integer phone = user.getPhone();
        if (phone != null) {
            tvPhone.setText(phone.toString());
        }

        tvEmail.setText(user.getEmail());
        tvBirthday.setText(user.getBirthday().getDate());
    }

    private void bindListener() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
